/**
 * CSV字段枚举分析工具
 * 统计每个字段的唯一值（枚举项）及其关系
 */

const fs = require('fs');
const Papa = require('papaparse');
const path = require('path');

/**
 * 判断是否为ID类字段（需要跳过的字段）
 */
function isIdField(columnName) {
    const idPatterns = [
        '_id',
        'id',
        '编号',
        '序号',
        'uuid',
        'guid'
    ];
    
    const lowerName = columnName.toLowerCase();
    return idPatterns.some(pattern => 
        lowerName.includes(pattern) && 
        (lowerName.endsWith('id') || lowerName.startsWith('id') || lowerName === '_id')
    );
}

/**
 * 分析CSV文件的枚举值
 */
function analyzeEnums(filePath) {
    return new Promise((resolve, reject) => {
        console.log(`\n分析文件: ${filePath}`);
        console.log('='.repeat(60));

        const enumStats = {};
        const relationshipStats = {};
        let totalRows = 0;
        let columns = [];
        
        const stream = fs.createReadStream(filePath, { encoding: 'utf-8' });

        Papa.parse(stream, {
            header: true,
            skipEmptyLines: true,
            
            step: function(result, parser) {
                totalRows++;

                // 初始化列信息
                if (totalRows === 1 && result.meta.fields) {
                    columns = result.meta.fields;
                    
                    columns.forEach(col => {
                        // 跳过ID类字段
                        if (isIdField(col)) {
                            console.log(`⏭️  跳过ID字段: ${col}`);
                            return;
                        }
                        
                        enumStats[col] = {
                            values: new Map(), // 值 -> 出现次数
                            total: 0,
                            empty: 0
                        };
                    });
                }

                // 收集枚举值
                columns.forEach(col => {
                    if (isIdField(col)) return;
                    
                    const value = result.data[col];
                    
                    if (!enumStats[col]) return;
                    
                    enumStats[col].total++;
                    
                    if (value === null || value === undefined || value === '' || value === '数据不存在') {
                        enumStats[col].empty++;
                    } else {
                        const count = enumStats[col].values.get(value) || 0;
                        enumStats[col].values.set(value, count + 1);
                    }
                });

                // 分析字段关系（例如：设施类型 -> 设施名称）
                // 假设第一个非ID字段是"父级"，其他非ID字段是"子级"
                const nonIdColumns = columns.filter(col => !isIdField(col));
                if (nonIdColumns.length >= 2) {
                    const parentCol = nonIdColumns[0]; // 设施类型
                    const childCol = nonIdColumns[1];  // 设施名称
                    
                    const parentValue = result.data[parentCol];
                    const childValue = result.data[childCol];
                    
                    if (parentValue && childValue && 
                        parentValue !== '数据不存在' && 
                        childValue !== '数据不存在') {
                        
                        if (!relationshipStats[parentCol]) {
                            relationshipStats[parentCol] = new Map();
                        }
                        
                        if (!relationshipStats[parentCol].has(parentValue)) {
                            relationshipStats[parentCol].set(parentValue, new Set());
                        }
                        
                        relationshipStats[parentCol].get(parentValue).add(childValue);
                    }
                }

                // 显示进度
                if (totalRows % 100000 === 0) {
                    process.stdout.write(`\r已分析: ${totalRows.toLocaleString()} 行`);
                }
            },

            complete: function() {
                console.log(`\n\n✅ 分析完成！共分析 ${totalRows.toLocaleString()} 行数据\n`);
                
                // 转换Map为普通对象便于处理
                const results = {
                    totalRows,
                    columns: columns.filter(col => !isIdField(col)),
                    enumStats: {},
                    relationshipStats: {}
                };

                // 处理枚举统计
                Object.keys(enumStats).forEach(col => {
                    const stats = enumStats[col];
                    const sortedValues = Array.from(stats.values.entries())
                        .sort((a, b) => b[1] - a[1]); // 按出现次数排序
                    
                    results.enumStats[col] = {
                        uniqueCount: stats.values.size,
                        total: stats.total,
                        empty: stats.empty,
                        values: sortedValues
                    };
                });

                // 处理关系统计
                Object.keys(relationshipStats).forEach(parentCol => {
                    const relationships = relationshipStats[parentCol];
                    results.relationshipStats[parentCol] = {};
                    
                    relationships.forEach((childSet, parentValue) => {
                        results.relationshipStats[parentCol][parentValue] = Array.from(childSet).sort();
                    });
                });

                resolve(results);
            },

            error: reject
        });
    });
}

/**
 * 生成枚举分析HTML报告
 */
function generateEnumHtmlReport(results, filePath, outputPath) {
    const fileName = path.basename(filePath);
    const fileSize = (fs.statSync(filePath).size / (1024 * 1024)).toFixed(2);
    
    // 生成枚举表格
    let enumTablesHtml = '';
    
    results.columns.forEach(col => {
        const stats = results.enumStats[col];
        if (!stats) return;
        
        const coverage = ((stats.total - stats.empty) / stats.total * 100).toFixed(2);
        
        enumTablesHtml += `
            <div class="enum-section">
                <h3>📌 ${col}</h3>
                <div class="enum-stats">
                    <span class="stat-badge">唯一值: <strong>${stats.uniqueCount}</strong></span>
                    <span class="stat-badge">总记录: <strong>${stats.total.toLocaleString()}</strong></span>
                    <span class="stat-badge">空值: <strong>${stats.empty.toLocaleString()}</strong></span>
                    <span class="stat-badge">覆盖率: <strong>${coverage}%</strong></span>
                </div>
                <div class="enum-table-wrapper">
                    <table class="enum-table">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>枚举值</th>
                                <th>出现次数</th>
                                <th>占比</th>
                                <th>可视化</th>
                            </tr>
                        </thead>
                        <tbody>
                            ${stats.values.slice(0, 100).map(([value, count], idx) => {
                                const percentage = (count / stats.total * 100).toFixed(2);
                                return `
                                    <tr>
                                        <td>${idx + 1}</td>
                                        <td class="enum-value">${value}</td>
                                        <td>${count.toLocaleString()}</td>
                                        <td>${percentage}%</td>
                                        <td>
                                            <div class="progress-bar">
                                                <div class="progress-fill" style="width: ${Math.min(percentage, 100)}%"></div>
                                            </div>
                                        </td>
                                    </tr>
                                `;
                            }).join('')}
                            ${stats.values.length > 100 ? `
                                <tr>
                                    <td colspan="5" class="more-info">
                                        ... 还有 ${stats.values.length - 100} 个枚举值（已省略）
                                    </td>
                                </tr>
                            ` : ''}
                        </tbody>
                    </table>
                </div>
            </div>
        `;
    });

    // 生成关系分析HTML
    let relationshipHtml = '';
    
    Object.keys(results.relationshipStats).forEach(parentCol => {
        const relationships = results.relationshipStats[parentCol];
        const childCol = results.columns[1]; // 假设第二列是子列
        
        relationshipHtml += `
            <div class="section">
                <h2>🔗 字段关系分析: ${parentCol} → ${childCol}</h2>
                <p class="section-desc">统计每个"${parentCol}"下有多少种不同的"${childCol}"</p>
                
                <div class="relationship-grid">
                    ${Object.entries(relationships)
                        .sort((a, b) => b[1].length - a[1].length)
                        .map(([parentValue, childValues]) => `
                            <div class="relationship-card">
                                <div class="relationship-header">
                                    <span class="parent-value">${parentValue}</span>
                                    <span class="child-count">${childValues.length} 种</span>
                                </div>
                                <div class="child-values">
                                    ${childValues.slice(0, 10).map(v => 
                                        `<span class="child-tag">${v}</span>`
                                    ).join('')}
                                    ${childValues.length > 10 ? 
                                        `<span class="more-tag">+${childValues.length - 10} 更多</span>` 
                                        : ''}
                                </div>
                            </div>
                        `).join('')}
                </div>
            </div>
        `;
    });

    const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字段枚举分析报告 - ${fileName}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
            line-height: 1.6;
        }
        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px;
            text-align: center;
        }
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        .header p {
            opacity: 0.9;
            font-size: 1.2em;
        }
        .content {
            padding: 40px;
        }
        .summary {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .summary-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            border-radius: 12px;
            text-align: center;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }
        .summary-card .label {
            font-size: 0.9em;
            opacity: 0.9;
            margin-bottom: 10px;
        }
        .summary-card .value {
            font-size: 2.5em;
            font-weight: bold;
        }
        .section {
            margin-bottom: 50px;
        }
        .section h2 {
            color: #333;
            margin-bottom: 15px;
            font-size: 1.8em;
            border-left: 5px solid #667eea;
            padding-left: 15px;
        }
        .section-desc {
            color: #666;
            margin-bottom: 25px;
            font-size: 1.1em;
        }
        .enum-section {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 12px;
            margin-bottom: 30px;
            border: 2px solid #e0e0e0;
        }
        .enum-section h3 {
            color: #667eea;
            margin-bottom: 15px;
            font-size: 1.5em;
        }
        .enum-stats {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }
        .stat-badge {
            background: white;
            padding: 8px 15px;
            border-radius: 8px;
            font-size: 0.95em;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .stat-badge strong {
            color: #667eea;
        }
        .enum-table-wrapper {
            overflow-x: auto;
            background: white;
            border-radius: 8px;
        }
        .enum-table {
            width: 100%;
            border-collapse: collapse;
        }
        .enum-table th {
            background: #667eea;
            color: white;
            padding: 12px;
            text-align: left;
            font-weight: 600;
        }
        .enum-table td {
            padding: 12px;
            border-bottom: 1px solid #e0e0e0;
        }
        .enum-table tr:hover {
            background: #f5f5f5;
        }
        .enum-value {
            font-weight: 600;
            color: #333;
        }
        .progress-bar {
            background: #e0e0e0;
            border-radius: 10px;
            height: 20px;
            overflow: hidden;
            width: 100%;
        }
        .progress-fill {
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            height: 100%;
            transition: width 0.3s;
        }
        .relationship-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 20px;
        }
        .relationship-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            border: 2px solid #e0e0e0;
            transition: transform 0.3s, box-shadow 0.3s;
        }
        .relationship-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
        }
        .relationship-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 2px solid #667eea;
        }
        .parent-value {
            font-size: 1.2em;
            font-weight: bold;
            color: #667eea;
        }
        .child-count {
            background: #667eea;
            color: white;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.9em;
            font-weight: bold;
        }
        .child-values {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
        }
        .child-tag {
            background: #f0f0f0;
            padding: 6px 12px;
            border-radius: 6px;
            font-size: 0.9em;
            color: #333;
        }
        .more-tag {
            background: #764ba2;
            color: white;
            padding: 6px 12px;
            border-radius: 6px;
            font-size: 0.9em;
        }
        .more-info {
            text-align: center;
            color: #666;
            font-style: italic;
            padding: 15px !important;
        }
        .footer {
            text-align: center;
            padding: 30px;
            background: #f8f9fa;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📊 字段枚举分析报告</h1>
            <p>CSV数据字典 · 枚举值统计 · 字段关系分析</p>
            <p style="margin-top: 10px; font-size: 1em;">${fileName}</p>
        </div>

        <div class="content">
            <!-- 汇总信息 -->
            <div class="summary">
                <div class="summary-card">
                    <div class="label">文件大小</div>
                    <div class="value">${fileSize} MB</div>
                </div>
                <div class="summary-card">
                    <div class="label">总行数</div>
                    <div class="value">${results.totalRows.toLocaleString()}</div>
                </div>
                <div class="summary-card">
                    <div class="label">分析字段数</div>
                    <div class="value">${results.columns.length}</div>
                </div>
                <div class="summary-card">
                    <div class="label">总枚举值</div>
                    <div class="value">${Object.values(results.enumStats).reduce((sum, s) => sum + s.uniqueCount, 0)}</div>
                </div>
            </div>

            <!-- 字段关系分析 -->
            ${relationshipHtml}

            <!-- 字段枚举详情 -->
            <div class="section">
                <h2>📋 字段枚举详情</h2>
                <p class="section-desc">每个字段的所有唯一值及其出现频率</p>
                ${enumTablesHtml}
            </div>
        </div>

        <div class="footer">
            <p>报告生成时间: ${new Date().toLocaleString('zh-CN')}</p>
            <p>CSV字段枚举分析工具</p>
        </div>
    </div>
</body>
</html>`;

    fs.writeFileSync(outputPath, html, 'utf-8');
}

/**
 * 主函数
 */
async function main() {
    const csvFiles = [
        'output/基本设施.csv',
        'output/设施集合.csv'
    ];

    console.log('='.repeat(60));
    console.log('CSV字段枚举分析工具');
    console.log('='.repeat(60));

    console.log('\n可用的CSV文件:');
    csvFiles.forEach((file, idx) => {
        try {
            const stats = fs.statSync(file);
            const sizeMB = (stats.size / (1024 * 1024)).toFixed(2);
            console.log(`${idx + 1}. ${file} (${sizeMB} MB)`);
        } catch (e) {
            console.log(`${idx + 1}. ${file} (文件不存在)`);
        }
    });

    // 分析第一个文件
    const inputFile = csvFiles[0];
    const outputFile = 'output/枚举分析报告.html';

    try {
        const results = await analyzeEnums(inputFile);
        
        console.log('\n生成HTML报告...');
        generateEnumHtmlReport(results, inputFile, outputFile);
        
        console.log(`\n✅ 报告已生成: ${outputFile}`);
        console.log('\n💡 双击打开该文件即可在浏览器中查看！\n');
        
    } catch (error) {
        console.error('\n❌ 分析失败:', error);
    }
}

if (require.main === module) {
    main();
}

module.exports = { analyzeEnums, generateEnumHtmlReport };

